Trò chơi Angry Birds trong UNITY Engine
31.652 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4
5 /// <summary>
6 /// based on the idea by Mike Talbot here http://whydoidoit.com/2012/04/01/smoothed-vector3-quaternions-and-floats-in-unity/
7 ///
8 /// lerps or slerps a Quaternion over time. usage is like so:
9 ///
10 /// mySmoothedQuat = target.rotation; // creates the GoSmoothedQuaternion
11 /// mySmoothedQuat.smoothValue = someNewQuaternion; // update the smoothValue whenever you would normally set the value on your object
12 /// target.rotation = mySmoothedQuat.smoothValue; // use the smoothValue property in an Update method to lerp/slerp it
13 ///
14 /// </summary>
15 public struct GoSmoothedQuaternion
16 {
17 public GoSmoothingType smoothingType;
18 public float duration;
19
20 private Quaternion _currentValue;
21 private Quaternion _target;
22 private Quaternion _start;
23 private float _startTime;
24
25
26 public GoSmoothedQuaternion( Quaternion quat )
27 {
28 _currentValue = quat;
29 _start = quat;
30 _target = quat;
31 _startTime = Time.time;
32
33 // set sensible defaults
34 duration = 0.2f;
35 smoothingType = GoSmoothingType.Lerp;
36 }
37
38
39 public Quaternion smoothValue
40 {
41 get
42 {
43 // how far along are we?
44 var t = ( Time.time - _startTime ) / duration;
45
46 switch( smoothingType )
47 {
48 case GoSmoothingType.Lerp:
49 _currentValue = Quaternion.Lerp( _start, _target, t );
50 break;
51 case GoSmoothingType.Slerp:
52 _currentValue = Quaternion.Slerp( _start, _target, t );
53 break;
54 }
55
56 return _currentValue;
57 }
58 set
59 {
60 _start = smoothValue;
61 _startTime = Time.time;
62 _target = value;
63 }
64 }
65
66
67 public float x
68 {
69 get
70 {
71 return _currentValue.x;
72 }
73 set
74 {
75 smoothValue = new Quaternion( value, _target.y, _target.z, _target.w );
76 }
77 }
78
79 public float y
80 {
81 get
82 {
83 return _currentValue.y;
84 }
85 set
86 {
87 smoothValue = new Quaternion( _target.x, value, _target.y, _target.w );
88 }
89 }
90
91 public float z
92 {
93 get
94 {
95 return _currentValue.z;
96 }
97 set
98 {
99 smoothValue = new Quaternion( _target.x, _target.y, value, _target.w );
100 }
101
102 }
103
104 public float w
105 {
106 get
107 {
108 return _currentValue.w;
109 }
110 set
111 {
112
113 smoothValue = new Quaternion( _target.x, _target.y, _target.z, value );
114 }
115 }
116
117
118 public static implicit operator GoSmoothedQuaternion( Quaternion q )
119 {
120 return new GoSmoothedQuaternion( q );
121 }
122
123 }
2 using System.Collections;
3
4
5 /// <summary>
6 /// based on the idea by Mike Talbot here http://whydoidoit.com/2012/04/01/smoothed-vector3-quaternions-and-floats-in-unity/
7 ///
8 /// lerps or slerps a Quaternion over time. usage is like so:
9 ///
10 /// mySmoothedQuat = target.rotation; // creates the GoSmoothedQuaternion
11 /// mySmoothedQuat.smoothValue = someNewQuaternion; // update the smoothValue whenever you would normally set the value on your object
12 /// target.rotation = mySmoothedQuat.smoothValue; // use the smoothValue property in an Update method to lerp/slerp it
13 ///
14 /// </summary>
15 public struct GoSmoothedQuaternion
16 {
17 public GoSmoothingType smoothingType;
18 public float duration;
19
20 private Quaternion _currentValue;
21 private Quaternion _target;
22 private Quaternion _start;
23 private float _startTime;
24
25
26 public GoSmoothedQuaternion( Quaternion quat )
27 {
28 _currentValue = quat;
29 _start = quat;
30 _target = quat;
31 _startTime = Time.time;
32
33 // set sensible defaults
34 duration = 0.2f;
35 smoothingType = GoSmoothingType.Lerp;
36 }
37
38
39 public Quaternion smoothValue
40 {
41 get
42 {
43 // how far along are we?
44 var t = ( Time.time - _startTime ) / duration;
45
46 switch( smoothingType )
47 {
48 case GoSmoothingType.Lerp:
49 _currentValue = Quaternion.Lerp( _start, _target, t );
50 break;
51 case GoSmoothingType.Slerp:
52 _currentValue = Quaternion.Slerp( _start, _target, t );
53 break;
54 }
55
56 return _currentValue;
57 }
58 set
59 {
60 _start = smoothValue;
61 _startTime = Time.time;
62 _target = value;
63 }
64 }
65
66
67 public float x
68 {
69 get
70 {
71 return _currentValue.x;
72 }
73 set
74 {
75 smoothValue = new Quaternion( value, _target.y, _target.z, _target.w );
76 }
77 }
78
79 public float y
80 {
81 get
82 {
83 return _currentValue.y;
84 }
85 set
86 {
87 smoothValue = new Quaternion( _target.x, value, _target.y, _target.w );
88 }
89 }
90
91 public float z
92 {
93 get
94 {
95 return _currentValue.z;
96 }
97 set
98 {
99 smoothValue = new Quaternion( _target.x, _target.y, value, _target.w );
100 }
101
102 }
103
104 public float w
105 {
106 get
107 {
108 return _currentValue.w;
109 }
110 set
111 {
112
113 smoothValue = new Quaternion( _target.x, _target.y, _target.z, value );
114 }
115 }
116
117
118 public static implicit operator GoSmoothedQuaternion( Quaternion q )
119 {
120 return new GoSmoothedQuaternion( q );
121 }
122
123 }